<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;

class Index extends  BaseModel{

	//检测参数是否已传
    protected $ParameterDetection  = [
    	'index'=>['search'],
    	'banner'=>['type']
    ];
	/**
	* 首页
	*/
	public function index(){
		$type = isset($this->requestData['type'])?$this->requestData['type']:'1';
		$page = isset($this->requestData['page'])?$this->requestData['page']:'1';

		$result = [];
		$where = [];
		$whereor = [];
		if(!empty($type))$where['type']=$type;

		$search = $this->requestData['search'];

		if(!empty($search)){
			$where['goods_search']=['like',"%$search%"];
			$whereor['circle_name']=['like',"%$search%"];
			$whereor['address']=['like',"%$search%"];
		}

		$where['is_del'] = '1';
		$where['is_index'] = '1';

		$data = db('circle')->where($where)->whereor($whereor)->page($page,'10')->order('id desc')->select();
		$banner_list = db('banner')->select();

//		foreach($data as $key => &$value){
//			$value['circle_image'] = IMG_PATH.$value['circle_image'];
//		}
		foreach($data as $key => $value){
			$data[$key]['circle_image'] = IMG_PATH.$data[$key]['circle_image'];
			$iskeepwhere['cid']=$value['id'];
			$iskeepwhere['user_id'] = $this->user['id'];

			if($type == '1'){
				$circle_goods = db('circle_goods');
			}

			if($type == '2'){
				$circle_goods = db('circle_goods_demand');
			}

			$circle_goods = $circle_goods->where(['cid'=>$value['id']])->order('id desc')->find();
			if($type == '1'){
				$circle_goods['price'] = $circle_goods['price']/100;
			}

			$circle_goods['image'] = IMG_PATH.$circle_goods['image'];

			if($circle_goods['images'] != ''){
				$circle_goods['images'] = json_decode($circle_goods['images'],1);
				foreach ($circle_goods['images']  as $k => &$v) {
					$v = IMG_PATH.$v;
				}
			}else{
				$circle_goods['images'] = [];
			}

			$data[$key]['circle_goods'] = $circle_goods;
			//是否收藏
			$iskeep = db('circle_keep')->where($iskeepwhere)->find();
			if($iskeep){
				$data[$key]['is_keep'] = 1;
			}else{
				$data[$key]['is_keep'] = 0;
			}

			//查询昵称跟用户名
			$member = db('member')->where(['id'=>$value['user_id']])->field('nickname,headimage')->find();
			$data[$key]['nickname'] = $member['nickname'];
			$data[$key]['headimage'] = $member['headimage'];
		}


		$result['circle_select'] = $data;
		$result['banner'] = $banner_list;

		return $result;
	}

	//轮播
	public function banner()
	{
		$type = $this->requestData['type'];
		if(!in_array($type, ['1','2']))return ['message'=>'类别参数有误!'];
		$data = db('banner')->where(['status'=>'1','type'=>$type])->order('sort desc')->select();
		foreach($data as $key => $value){
			$data[$key]['img'] = IMG_PATH.$data[$key]['img'];
		}
		return $data;
	}
}